<template>
  <view class="wallet">
    <view class="container">
      <view class="money">
        <view class="num">¥{{money.all_money.toFixed(2)}}</view>
        <view class="backMoney"><AtButton size="small"  type='primary' @click="getMoneyBack">立即返还</AtButton></view>
      </view>
      <view class="content">
        <view v-if="!noDataTrue" class="orderItem" v-for="item in OrderRecord" :key="item.withdrawal_code">
          <view class="topStatus">
            <view class="orderCode">提现码: <text style="font-size: 24rpx; position: relative; top: -5rpx;">{{item.withdrawal_code}}</text></view>
            <view class="status" :class="item.w_status == -1 ? 'cancleOrder': item.w_status == 0 ? 'willPayOrder' : item.w_status == 1 ? 'willGetGlass' : 'alreadyComplate'">
              {{item.w_status == -1 ?  '驳回' : item.w_status == 0 ? '待审核' : item.w_status == 1 ? '正在提现' : item.w_status == 2 ? '已到账' : ''}}
            </view>
          </view>
          <view class="middleContent">
            <view class="order-item">提现金额: ¥{{ item.total_price }}</view>
            <view class="order-item">提现时间: {{ item.withdrawal_date }}</view>
          </view>
        </view>
        <view class="no_list" v-else="noDataTrue"></view>
      </view>
      <AtModal :isOpened="getMoneyIsOpened" @close="getMoneyIsOpened = false" @confirm="getMoneyIsOpened = false" @cancel="getMoneyIsOpened = false">
        <AtModalHeader>押金提现</AtModalHeader>
        <AtModalContent>
          <view class="mainContant">
            <view class="phone" style="text-align: left; text-indent: 2em; margin-bottom: 30px;">当前可提现金额为{{money.t_money}}, 是否进行余额提现？提现金额预计72小时内到账，请耐心等待~</view>
            <view class="adContainer"></view>
            <ad-custom
              v-if="getMoneyIsOpened"
              unit-id="adunit-1e7d68ee863117c9"
              ad-intervals="60"
              :scale="80"
              @load="onLoadAD"
              @error="onErrorAD"
            />
          </view>
        </AtModalContent>
        <AtModalAction>
          <button @tap="getMoneyIsOpened = false">取消</button>
          <button @tap="getAllmoney">确认</button>
        </AtModalAction>
      </AtModal>
    </view>
  </view>
</template>

<script setup>
import './index.scss'
import { AtButton, AtModal, AtModalHeader, AtModalContent, AtModalAction } from 'taro-ui-vue3'
import {query_balance, do_refund, query_refund_record} from '../../http/api'
import {ref, reactive} from 'vue'
import Taro, {useDidShow} from '@tarojs/taro'
const money = reactive({
  all_money: 0,
  t_money: 0
})

const noDataTrue = ref(false)
function onLoadAD () {
  console.log('Banner 广告加载成功')
}

function onErrorAD (err) {
  console.error('Banner 广告加载失败', err)
}
const getMoneyIsOpened = ref(false)
function getAllmoney () {
  if (money.t_money <= 0) {
    Taro.showToast({
      title: '当前可提现余额为0',
      icon: 'none',
      duration: 2000
    })
    getMoneyIsOpened.value = false;
    return
  }
  do_refund().then(money => {
    if(money.code === '1000') {
      getMoneyIsOpened.value = false;
      Taro.showToast({
        title: '提现申请成功，金额返还付款账户',
        icon: 'success',
        duration: 2000
      })
      getMoney()
    } else {
      Taro.showToast({
        title: `提现失败`,
        icon: 'error',
        duration: 2000
      })
    }
  })
}

function getMoneyBack() {
  getMoneyIsOpened.value = true;

//   Taro.showModal({
//     title: '余额提现',
//     confirmColor:'#ffc600',
//     content: `当前可提现金额为¥${money.t_money}, 是否进行余额提现？提现金额预计72小时内到账，请耐心等待~`,
//     success: function (res) {
//       if (res.confirm) {
//         do_refund().then(money => {
//           if(money.code === '1000') {
//             Taro.showToast({
//               title: '提现申请成功，金额返还付款账户',
//               icon: 'success',
//               duration: 2000
//             })
//             getMoney()
//           } else {
//             Taro.showToast({
//               title: `${money.msg}`,
//               icon: 'error',
//               duration: 2000
//             })
//           }
//         })
//       } else if (res.cancel) {
//         console.log('用户点击取消')
//       }
//     }
// })
}


function getMoney () {
  query_balance().then(res => {
    money.all_money = res.data.all_money
    money.t_money = res.data.t_money
  })
}
const OrderRecord = ref([])
function returnOrderRecord () {
  query_refund_record().then(res => {
    if (res.code == '1000') {
      OrderRecord.value = res.data
      // OrderRecord.value = [{
      //   "total_price": 2,
      //   "w_status": -1,
      //   "withdrawal_code": "W202405281641289269226xmo8n",
      //   "withdrawal_date": "2024-05-28 16:41:29"
      // }]
      noDataTrue.value =  !OrderRecord.value.length
    }
  })
}


function gotoPayHtml(code) {
  Taro.navigateTo({
    url: `/pages/orderDetail/index?o_order_code=${code}&o_is_refund=-1`
  })
}

useDidShow(() => {
  getMoney()
  returnOrderRecord()
})
Taro.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})
</script>
